<template>
  <div class="content" v-loading="ishow">
    <el-card>
      <el-row>
        <el-col :span="6">
          <span class="strong">工号:</span>
          <el-input v-model="params.teaSno" placeholder="请输入工号" style="width: 230px;" size="small" clearable></el-input>
        </el-col>
        <el-col :span="6">
          <span class="strong">姓名:</span>
          <el-input v-model="params.teaName" placeholder="请输入辅导员姓名" style="width: 230px;" size="small"
            clearable></el-input>
        </el-col>
        <el-col :span="6">
          <span class="strong">qq号:</span>
          <el-input v-model="params.teaQq" placeholder="请输入qq号" style="width: 230px;" size="small" clearable></el-input>
        </el-col>
        <el-col :span="6">

          <span class="strong">邮箱:</span>
          <el-input v-model="params.teaEmail" placeholder="请输入邮箱" style="width: 230px;" size="small"
            clearable></el-input>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px;">
        <el-col :span="6">

          <span class="strong">办公室:</span>
          <el-input v-model="params.teaOffice" placeholder="请输入办公室" size="small" style="width: 230px;"
            clearable></el-input>
        </el-col>
        <el-col :span="6">

          <span class="strong">政治面貌:</span>
          <el-select v-model="params.teaPolitics" placeholder="请选择政治面貌" style="width: 180px;" size="small" clearable>
            <el-option v-for="item in Politics" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">

          <span class="strong">性别:</span>
          <el-select v-model="params.teaSex" placeholder="请选择性别" style="width: 230px;" size="small" clearable>
            <el-option key="1" label="男" value="男">
            </el-option>
            <el-option key="2" label="女" value="女">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" style="margin: 0 10px;" @click="getList" size="small"
            v-if="$hasBP('btn.teacher.select')">查询</el-button>
          <el-button type="success" @click="addTeacher" size="small" v-if="$hasBP('btn.teacher.add')">增加</el-button>
          <el-button @click="clear" size="small" v-if="$hasBP('btn.teacher.select')">重置</el-button>
        </el-col>
      </el-row>



      <el-row style="margin-top: 10px;">
        <el-table ref="multipleTable" :data="tableData" style="width: 100%" border>
          <el-table-column prop="teaSno" fixed label="铺导员工号" width="100" align="center">
          </el-table-column>
          <el-table-column prop="teaName" label="铺导员名称" width="120" align="center">
          </el-table-column>
          <el-table-column prop="teaSex" label="性别" width="80" align="center">
          </el-table-column>
          <el-table-column prop="teaQq" label="QQ号" width="180" align="center">
          </el-table-column>
          <el-table-column prop="teaEmail" label="邮箱" width="200" align="center">
          </el-table-column>
          <el-table-column prop="teaPhone" label="手机号" width="200" align="center">
          </el-table-column>
          <el-table-column prop="teaPolitics" label="政治面貌" width="150" align="center">
          </el-table-column>
          <el-table-column prop="teaOffice" label="办公室" width="130" align="center">
          </el-table-column>
          <el-table-column prop="teaAcademic" label="学历" width="100" align="center">
          </el-table-column>
          <el-table-column prop="createName" label="创建人" width="100" align="center">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="200" align="center">
            <template slot-scope="scope">{{ scope.row.createTime | formatCreateTimes }}</template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200" fixed="right">
            <template #default="{row,$index}">
              <el-button type="warning" size="small" @click="updateClass(row)"
                v-if="$hasBP('btn.teacher.update')">修改</el-button>
              <el-button type="danger" size="small" @click="delTeacher(row)"
                v-if="$hasBP('btn.teacher.del')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <!-- v-if="total>5" -->
      <el-row>
        <div style="float: right;margin-top: 10px;">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="params.pageNum" :page-sizes="[1, 5, 10]" :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-row>
    </el-card>


    <el-dialog title="铺导员操作" :visible.sync="isAddorUpdateShow" width="50%">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="铺导员工号" prop="teaSno">
          <el-input v-model="form.teaSno" :disabled="isAddOrUpdate==1" size="small"></el-input>
        </el-form-item>
        <el-form-item label="铺导员名称" prop="teaName">
          <el-input v-model="form.teaName" size="small"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="teaSex">
          <el-select v-model="form.teaSex" placeholder="请选择性别" style="width: 230px;" size="small">
            <el-option key="1" label="男" value="男">
            </el-option>
            <el-option key="2" label="女" value="女">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="QQ号">
          <el-input v-model="form.teaQq" size="small"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="teaEmail">
          <el-input v-model="form.teaEmail" size="small"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="teaPhone">
          <el-input v-model="form.teaPhone" size="small"></el-input>
        </el-form-item>
        <el-form-item label="政治面貌">
          <el-select v-model="form.teaPolitics" placeholder="请选择政治面貌" style="width: 230px;" size="small">
            <el-option v-for="item in Politics" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="办公室" prop="teaOffice">
          <el-input v-model="form.teaOffice" size="small"></el-input>
        </el-form-item>
        <el-form-item label="学历" prop="teaAcademic">
          <el-input v-model="form.teaAcademic" size="small"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isAddorUpdateShow = false">取 消</el-button>
        <el-button type="primary" @click="affirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    formatDate
  } from '../../../utils/date.js'

  export default {
    name: 'class',
    data() {
      return {
        params: {
          pageNum: 1,
          pageSize: 5,
          keyWord: ""
        },
        tableData: [],
        isAddOrUpdate: 0,//0添加1修改
        isAddorUpdateShow: false,
        form: {
          cteaSex: "",
          teaPolitics: ""
        },
        teacherList: [],
        total: 0,
        ishow: true,
        Politics: [
          {
            id: 1,
            name: "群众"
          },
          {
            id: 2,
            name: "共青团员"
          },
          {
            id: 3,
            name: "党员"
          },

        ],
        rules: {
          teaSno: [
            { required: true, message: '请输入工号', trigger: 'blur' }
          ],
          teaName: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          teaSex: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          teaPhone: [
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ],
          teaOffice: [
            { required: true, message: '请输入办公室', trigger: 'blur' }
          ],
          teaAcademic: [
            { required: true, message: '请输入学历', trigger: 'blur' }
          ],
          teaEmail: [
            { required: true, message: '请输入邮箱', trigger: 'blur' }
          ],
        }
      };
    },

    mounted() {
      this.getList()
      this.getTeacherList()
    },
    filters: {
      formatCreateTimes(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd  hh:mm:ss')
      }
    },
    methods: {
      getList() {
        this.$api.teacher.getTearcherList(this.params).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.tableData = res.data
            this.total = res.pageParams.total
            this.ishow = false
          }
        })
      },
      getTeacherList() {
        this.$api.common.selectListofTeacher().then(res => {
          if (res.code == 200) {
            this.teacherList = res.data
          }
        })
      },
      affirm() {
        let reg = /^1[13456789]\d{9}$/
        if (!reg.test(this.form.teaPhone)) {
          this.$message.error('手机号格式错误')
          return
        }
        if (this.isAddOrUpdate == 0) {//添加
          this.$api.teacher.addTearcherList(this.form).then(res => {
            if (res.code == 200) {
              this.$message.success("添加成功~")
              this.getList()
              this.isAddorUpdateShow = false
            }
          })
        } else if (this.isAddOrUpdate == 1) {
          this.$api.teacher.updateTearcherList(this.form).then(res => {
            if (res.code = 200) {
              this.$message.success("修改成功~")
              this.getList()
              this.isAddorUpdateShow = false
            }
          })

        }
      },
      addTeacher() {
        this.isAddOrUpdate = 0;
        this.isAddorUpdateShow = true;
        this.form = {}
      },
      updateClass(data) {
        console.log(data);
        this.form = JSON.parse(JSON.stringify(data))
        this.isAddorUpdateShow = true
        this.isAddOrUpdate = 1
      },
      handleSizeChange(val) {
        console.log(val);
        this.params.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.params.pageNum = val
        this.getList()
      },
      delTeacher(row) {
        this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(res => {
          this.$api.teacher.delTearcherList(row.id).then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$message.success("删除成功~")
              this.getList()
            }
          })
        }).catch(() => { })
      },
      clear() {
        this.params = {}
        this.params.pageNum = 1
        this.params.pageSize = 10
        this.getList()
      }

    },
  };
</script>

<style lang="scss" scoped>
  .content {
    padding: 10px;
  }

  .strong {
    font-weight: 600;
    font-size: 14px;
    color: #606266;
  }
</style>